//-----------------------------------------------------------------------

// <copyright file="StringListFieldDefinition.cs" company="Jamie Knight">

// Copyright (c) Jamie Knight. All rights reserved.

// </copyright>

//-----------------------------------------------------------------------

using System;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace DevKnightlie.WPF.AutoForm.FieldDefinitions
{
    public class StringListFieldDefinition:BaseFieldDefinition
    {
        #region Private Fields

        private string _selectedIndexPropertyName;
        private string _selectedItemPropertyName;


        #endregion


        #region Properties

        /// <summary>
        /// Sets or returns the name of a property the ListBox SelectedIndex property should be bound to
        /// </summary>
        public String SelectedIndexPropertyName
        {
            get { return _selectedIndexPropertyName; }
            set { _selectedIndexPropertyName = value; }
        }


        /// <summary>
        /// Sets or returns the name of a property the ListBox SelectedItem property should be bound to
        /// </summary>
        public String SelectedItemPropertyName
        {
            get { return _selectedItemPropertyName; }
            set { _selectedItemPropertyName = value; }
        }


        #endregion


        #region Methods

        public override void BuildInputControl()
        {
            ListBox listBox = new ListBox()
            {
                Height=80 // TODO: Magic number should not be arbitrary
            };
            Binding itemsSourceBinding = new Binding(PropertyName);
            BindingOperations.SetBinding(listBox, ItemsControl.ItemsSourceProperty, itemsSourceBinding);
            SetupEnabledBinding(listBox);
            if (!String.IsNullOrWhiteSpace(SelectedIndexPropertyName))
            {
                Binding indexBinding = new Binding(SelectedIndexPropertyName);
                BindingOperations.SetBinding(listBox, Selector.SelectedIndexProperty, indexBinding);
            }
            if (!String.IsNullOrWhiteSpace(SelectedItemPropertyName))
            {
                Binding itemBinding = new Binding(SelectedIndexPropertyName);
                BindingOperations.SetBinding(listBox, Selector.SelectedItemProperty, itemBinding);
            }
            ContainerControl = InputControl = listBox;
        }

        #endregion
    }
}
